package pckDao;

import java.io.Serializable;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import pckEntidad.Linea;
import pckUtil.Conexion;

public class LineaDao implements Serializable {

    private Conexion oConexion=new Conexion();
    private Linea oLinea = null;
    private List<Linea> lisLinea=null;

   public Linea obtener(String codigo) {
         String sql = "select * from tbllinea where idline=?";
        try {
            PreparedStatement pst = oConexion.getConnection().prepareStatement(sql);                
            pst.setString(1, codigo);
            ResultSet rst = pst.executeQuery();
            
            while(rst.next()){
                oLinea=new Linea();
                oLinea.setIdline(rst.getString(1));
                oLinea.setNomblin(rst.getString(2));
                oLinea.setDesclin(rst.getString(3));
            }
            return  oLinea;
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage());
        }
         return null;
     }
    
    
    public void insertar(Linea oLin){
         String sql = "{call sping_Linea(?,?,?)}";
         CallableStatement cst;
        try {
            cst = oConexion.getConnection().prepareCall(sql);
            cst.setString(1, oLin.getNomblin());
            cst.setString(2, oLin.getDesclin());
            cst.setString(3, oLin.getCategoria().getIdline());
            cst.execute();            
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage());
        }   
     }
    
    
    public List<Linea> listar(){
    String sql = "{call splis_Linea}";
        try {
            CallableStatement cst = oConexion.getConnection().prepareCall(sql);
            ResultSet rst = cst.executeQuery();
            lisLinea= new ArrayList<Linea>();
            while(rst.next()){
                oLinea = new Linea();
                oLinea.setIdline(rst.getString(1));  
                oLinea.setNomblin(rst.getString(2));
                oLinea.setDesclin(rst.getString(3));
                lisLinea.add(oLinea);
            }
            return  lisLinea;
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage());
        }
         return null;
     }
    
    
    public void actualizar( Linea oLin){
        try {
            String sql = "{call spact_tblLinea(?,?,?,?)}";
            CallableStatement cst = oConexion.getConnection().prepareCall(sql);
            cst.setString(1, oLin.getIdline());
            cst.setString(2, oLin.getNomblin());
            cst.setString(3, oLin.getDesclin());
            cst.setString(4, oLin.getCategoria().getIdline());
            cst.execute();
            
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage());
        }
         
     }
    
}
